//
//  ADDetailsViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/7/22.
//  Copyright (c) 2015年 easaa. All rights reserved.
//
float fontSize;
#import "ADDetailsViewController.h"

@interface ADDetailsViewController ()
{
    UIWebView *_picWebView;
    UITextView *textView;
}
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSMutableArray *imageListMutableArray;
@property (nonatomic,strong)NSMutableArray *imageMutableArray;
@end

@implementation ADDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [NSMutableArray new];
    _imageListMutableArray = [NSMutableArray new];
    _imageMutableArray = [NSMutableArray new];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self requestData];
    
//    [self creatUI];
}

-(void)creatUI
{
    NSDictionary *dic = [_dataArr firstObject];
    NSMutableString *htmlString = [[NSMutableString alloc]init];
    //取的内容和图片的字段
    NSString *str1 = [dic[@"content"] stringByReplacingOccurrencesOfString:@"+" withString:@""];
    [htmlString setString :str1];

    
    //加点击事件，取所有图片------------------begin--------------------
    NSRange resultRange = NSMakeRange(0, 0);
    do
    {
        resultRange = [htmlString rangeOfString:@"<img.*?>" options:NSRegularExpressionSearch range:NSMakeRange(resultRange.location + resultRange.length, htmlString.length - resultRange.location - resultRange.length)];
        if (resultRange.length != 0)
        {
            NSLog(@"%li,%@",resultRange.location, [htmlString substringWithRange:resultRange]);
            BOOL isHaveSameString = NO;
            for(NSDictionary *tempDictionary in _dataArr)
            {
                if ([[tempDictionary objectForKey:[NSString stringWithFormat:@"%@",@"description"]] isEqualToString:[htmlString substringWithRange:resultRange]])
                {
                    isHaveSameString = YES;
                }
            }
            
            if (isHaveSameString == NO)
            {
                NSString *imageString = [htmlString substringWithRange:resultRange];
                NSRange imageRange;
                imageRange.location = 0;
                imageRange.length = imageString.length;
                imageRange = [imageString rangeOfString:@"src" options:NSCaseInsensitiveSearch range:imageRange];
                imageRange.location = imageRange.location + imageRange.length;
                imageRange.length = imageString.length - imageRange.location;
                
                NSString *srcString = [imageString substringWithRange:imageRange];
                imageRange.location = 0;
                imageRange.length = srcString.length;
                NSRange srcBeginRange = [srcString rangeOfString:@"\"" options:NSCaseInsensitiveSearch range:imageRange];
                
                if (imageRange.length > 2)
                {
                    imageRange.location = srcBeginRange.location + 1;
                    imageRange.length = srcString.length - imageRange.location;
                }
                
                NSRange srcEndRange = [srcString rangeOfString:@"\"" options:NSCaseInsensitiveSearch range:imageRange];
                if (srcBeginRange.length > 0 && srcEndRange.length > 0)
                {
                    imageRange.location = srcBeginRange.location + srcBeginRange.length;
                    imageRange.length = srcEndRange.location - imageRange.location;
                    srcString = [srcString substringWithRange:imageRange];
                }
                else
                {
                    
                }
                NSString *hrefStirng = nil;
                NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc]init];
                if (imageString != nil)
                {
                    [dataDictionary setObject:imageString forKey:@"image"];
                }
                if (srcString != nil)
                {
                    //                    hrefStirng = [NSString stringWithFormat:@"<img src=\"%@\">%@</a>",srcString,imageString];
                    hrefStirng = [NSString stringWithFormat:@"<img src=\"%@\">",srcString];
                    
                    [dataDictionary setObject:srcString forKey:@"scr"];
                    [dataDictionary setObject:hrefStirng forKey:@"img"];
                    [_imageListMutableArray addObject:srcString];
                }
                [_imageMutableArray addObject:dataDictionary];
            }
        }
        
        
    }
    while (resultRange.length != 0);
    //加点击事件，取所有图片------------------end--------------------
    for(NSDictionary *tempDictionary in _imageMutableArray)
    {
        [htmlString replaceOccurrencesOfString:[tempDictionary objectForKey:@"image"] withString:[tempDictionary objectForKey:@"img"] options:NSCaseInsensitiveSearch range:NSMakeRange(0, htmlString.length)];
    }
    DLog(@"加入链接后：%@",htmlString);
    
    NSString *temstr=@"<style type=\"text/css\">img{max-width:100%;height:auto;} </style>";//只要是img标签都限定大小
    //     NSString *fontString=@"<style type=\"text/css\">*{font-size:14px; color:#FFFFFF} </style>";//任何(*)标签都设置字体--本项目用的标签基本都是<p>,所以可以把*改成p
    
//    NSString *fontString = [NSString stringWithFormat:@"<style type=\"text/css\">*{font-size:%fpx;color:#6d6d6d; } </style>",fontSize];//line-height:20
//    
//    NSString *str = [NSString stringWithFormat:@"<body style=\"background-color: white;margin-right:16px;margin-left:16px;;\">"];
//    
//    NSString *htmlString1 = [NSString stringWithFormat:@"%@%@%@%@" ,temstr, fontString, htmlString,str];//设置大小
    

    _picWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    [_picWebView loadHTMLString:htmlString baseURL:nil];
    [self.view addSubview:_picWebView];

}



- (void)requestData {
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"ad_id":_adID};
    NSLog(@"广告详情页%@",parameters);
    [manager GetWithParameters:parameters method:@"search.ad.list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"广告的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            [_dataArr setArray:dictionary[@"data"]];
            [self creatUI];
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
